﻿//// Copyright 2005, 2006 - Morten Nielsen (www.iter.dk)
////
//// This file is part of SharpMap.
//// SharpMap is free software; you can redistribute it and/or modify
//// it under the terms of the GNU Lesser General Public License as published by
//// the Free Software Foundation; either version 2 of the License, or
//// (at your option) any later version.
//// 
//// SharpMap is distributed in the hope that it will be useful,
//// but WITHOUT ANY WARRANTY; without even the implied warranty of
//// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//// GNU Lesser General Public License for more details.

//// You should have received a copy of the GNU Lesser General Public License
//// along with SharpMap; if not, write to the Free Software
//// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
//// for petra.gis adopted by asida

//using System;
//using System.IO;
//using System.Runtime.Serialization;
//using System.Runtime.Serialization.Json;
//using System.Windows.Markup;
//using System.Collections.Generic;
//using System.Text;
//using Petra.Gis.Geometry.Support;
////using Petra.Gis.ReferenceSystemServices.Transformations;

//namespace Petra.Gis.ReferenceSystemServices
//{
  
//  /// <summary>
//  /// Base interface for all coordinate systems.
//  /// </summary>
//  /// <remarks>
//  /// <para>A coordinate system is a mathematical space, where the elements of the space
//  /// are called positions. Each position is described by a list of numbers. The length 
//  /// of the list corresponds to the dimension of the coordinate system. So in a 2D 
//  /// coordinate system each position is described by a list containing 2 numbers.</para>
//  /// <para>However, in a coordinate system, not all lists of numbers correspond to a 
//  /// position - some lists may be outside the domain of the coordinate system. For 
//  /// example, in a 2D Lat/Lon coordinate system, the list (91,91) does not correspond
//  /// to a position.</para>
//  /// <para>Some coordinate systems also have a mapping from the mathematical space into 
//  /// locations in the real world. So in a Lat/Lon coordinate system, the mathematical 
//  /// position (lat, long) corresponds to a location on the surface of the Earth. This 
//  /// mapping from the mathematical space into real-world locations is called a Datum.</para>
//  /// </remarks>
//  public abstract partial class CoordinateSystem
//  {

//    private static CoordinateSystemCache _CrsCache;


//    /// <summary>
//    /// static constructor initializes CoordinateSystem cache
//    /// to search for predefined CRS by various criteria
//    /// </summary>
//    static CoordinateSystem()
//    {
//      //CoordinateSystemCache _cstCache;

//      // load transform definition xml file

//      //string path;

//      //#if (DEBUG)
//      //      //path = "CoordinateSystemCache.xcs";
//      //      path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
//      //      path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.ToString(), 
//      //             @"..\..\CoordinateSystemCache.xcs");
//      //#else
//      // create some object from this assembly, to get path from reflection
//      // _cstCache must be located with this dll
//      //System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
//      //System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);

//      Axis s = new Axis();
//      System.Reflection.Assembly asm = System.Reflection.Assembly.GetAssembly(s.GetType());
//      System.IO.FileInfo f = new System.IO.FileInfo(asm.Location);

//      //path = f.Directory + "\\CoordinateSystemCache.xcs";
      
//      //#endif

//      //System.Xml.XmlDocument data = new System.Xml.XmlDocument();
//      //data.Load(path);
//      //FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
      

//      //_CrsCache = (CoordinateSystemCache)XamlReader.Load(fs);
//      //CoordinateSystems = _cstCache.CoordinateSystems;
//      //fs.Close();

//      //create empty cache here, if no file with predefined SRS exists, this remains empty
//      _CrsCache = new CoordinateSystemCache();

//      string pathJson = f.Directory + "\\CoordinateSystemCache.json";
//      if (!System.IO.File.Exists(pathJson))
//        return;

//      //json
//      Type[] t = new Type[]
//      {
//        typeof(ProjectedCoordinateSystem),
//        typeof(GeographicCoordinateSystem),
//        typeof(GeocentricCoordinateSystem)
//      };
      
//      FileStream fsj = new FileStream(pathJson, FileMode.Open, FileAccess.Read);
//      DataContractJsonSerializer outp = new DataContractJsonSerializer(typeof(CoordinateSystemCache));
//      _CrsCache = (CoordinateSystemCache)outp.ReadObject(fsj);
//      fsj.Close();

      
//    }






//    #region staticMembers


//    ///// <summary>
//    ///// returns reference to global CoordinateSystemCache object
//    ///// that maintains all current release of Petra.Gis coordinate systems.
//    ///// </summary>
//    //public static CoordinateSystemCache SRSCache
//    //{
//    //  get { return _CrsCache; }
//    //}









//    /// <summary>
//    /// 
//    /// </summary>
//    /// <param name="authority">coordinate system authority name</param>
//    /// <param name="code">coordinate system code</param>
//    /// <param name="storageUnitName">name of storage units</param>
//    /// <returns>if any predefined coordinate system in cache meets search criteria
//    /// then first matching coordinate system is returned. If no match, returns null.
//    /// </returns>
//    public static CoordinateSystem SearchBy(string authority, string code, string storageUnitName)
//    {

//      //CoordinateSystem result = null;

//      // search first in coordinate systems...
//      foreach (CoordinateSystem cs in _CrsCache.CoordinateSystems)
//      {
//        if (cs.Metadata.Authority == authority && cs.Metadata.AuthorityCode.ToString() == code)
//          return _resolveNamedCsStorageUnits(cs, storageUnitName);

//      }

//      // ok, not found
//      // try search in transforms collection...
//      //foreach (SRSTransform t in SRSTransform.Transforms)
//      //{
//      //  if (t.Source.Metadata.Authority == authority && t.Source.Metadata.AuthorityCode.ToString() == code)
//      //    //result =  t.Source;
//      //    return _resolveNamedCsStorageUnits(t.Source, storageUnitName);

//      //  else if (t.Target.Metadata.Authority == authority && t.Target.Metadata.AuthorityCode.ToString() == code)
//      //    //result = t.Target;
//      //    return _resolveNamedCsStorageUnits(t.Target, storageUnitName);
//      //}

//      // if coordinate system found
//      // and units are specified as input parameter
//      // try change coordinate system units
//      //if (result != null && storageUnitName != "")
//      //{
//      //  // try resolve storage units..
//      //  return resolveNamedCsStorageUnits(result, storageUnitName);
//      //}

//      return null;
//    }




//    /// <summary>
//    /// if coordinate system found and storage unist differs from search request
//    /// coordinate system definition is updated by requested storage units
//    /// </summary>
//    /// <param name="cs">coordinate system to update</param>
//    /// <param name="inputStorageUnitName">name of storage units to replace original with</param>
//    /// <returns>updated coordinate system. If requested units not found or units are different (angular x distance) returns null</returns>
//    private static CoordinateSystem _resolveNamedCsStorageUnits(CoordinateSystem cs, string inputStorageUnitName)
//    {
//      // if coordinate system found
//      // and units are specified as input parameter
//      if (inputStorageUnitName == "")
//        return cs;

//      // try change coordinate system units
//      // try resolve storage units..
//      // search for unit by given name
//      MeasureUnit su = MeasureUnit.Parse(inputStorageUnitName);

//      // if unit not found, return null
//      if (su == null)
//        return null;

//      // if some unit found, check for sure
//      // if type is same (angular or distance)
//      if (cs.Unit.GetType() != su.GetType())
//        return null;
//      //throw new ArgumentException("specified coordinate system in:" + csInfo +
//      //                            " has " + su.GetType().ToString() + " storage units  and " +
//      //                            " found coordinate system has " + result.Unit.GetType().ToString());

//      // if given unit is different from cs one
//      // clone cs and replace
//      if (!cs.Unit.EqualParams(su))
//      {
//        CoordinateSystem result = cs.Clone();
//        result.Unit = su;
//        return result;
//      }
//      else
//        // or leave and return cs untouched
//        return cs;

//    }


//    # endregion staticMembers


//  }
//}
